﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCTAccess.Business;
using DCTLib.Common;
using Library.DCTAccess.Business.Report;
using System.Collections.Generic;

public partial class AdminZone_SalesReport : System.Web.UI.Page
{
    protected int startYear = 2000;
    protected int maxYear = 0;
    protected string getYear = "2000";
    protected decimal total = 0;
    protected decimal currAmount = 0;
    protected int currMonth = 0;
    protected decimal jan = 0, feb = 0, mar = 0, apr = 0, may = 0, jun = 0, jul = 0, aug = 0, sep = 0, octo = 0, nov = 0, dec = 0;
    protected decimal Jan = 0, Feb = 0, Mar = 0, Apr = 0, May = 0, Jun = 0, Jul = 0, Aug = 0, Sep = 0, Octo = 0, Nov = 0, Dec = 0;

    private string delimiter = "/";
    protected Report rp;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    protected void BindData()
    {
        string today = DateTime.Now.ToShortDateString();  //format Date mmDDYYYY
        string[] values = StringHelper.Split(today.Trim(), delimiter);
        maxYear = int.Parse(values[2]);

        for (int i = startYear; i <= maxYear; i++)
        {
            DropDownList1.Items.Add(i.ToString());
        }
    }

    protected void CreateReport(int iYear)
    {
        DataTable dtValue = Report.GetReportByFilter(iYear);
        if (dtValue == null || dtValue.Rows.Count == 0)
        {
            lblInfo.Text = "No data record.";
        }
        else
        {
            lblInfo.Text = "";
            foreach (DataRow dr in dtValue.Rows)
            {
                currAmount = Convert.ToDecimal(dr[1].ToString());
                currMonth = Convert.ToInt32(dr[0].ToString());
                total = total + currAmount;

                try
                {
                    switch (currMonth)
                    {
                        case 1: jan = currAmount; Jan = (jan / total) * 100; break;
                        case 2: feb = currAmount; Feb = (jan / total) * 100; break;
                        case 3: mar = currAmount; Mar = (mar / total) * 100; break;
                        case 4: apr = currAmount; Apr = (apr / total) * 100; break;
                        case 5: may = currAmount; May = (may / total) * 100; break;
                        case 6: jun = currAmount; Jun = (jun / total) * 100; break;
                        case 7: jul = currAmount; Jul = (jul / total) * 100; break;
                        case 8: aug = currAmount; Aug = (aug / total) * 100; break;
                        case 9: sep = currAmount; Sep = (sep / total) * 100; break;
                        case 10: octo = currAmount; Octo = (octo / total) * 100; break;
                        case 11: nov = currAmount; Nov = (nov / total) * 100; break;
                        case 12: dec = currAmount; Dec = (dec / total) * 100; break;
                    }
                }
                catch (System.Exception ex)
                {
                    Jan = 0; Feb = 0; Mar = 0; Apr = 0;
                    May = 0; Jun = 0; Jul = 0; Aug = 0;
                    Sep = 0; Octo = 0; Nov = 0; Dec = 0;
                }
            }
        }
    }

    //no use
    public static bool isNumeric(string isValue)
    {
        try
        {
            Convert.ToInt32(isValue);
            return true;
        }
        catch (System.Exception ex)
        {
            return false;
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        getYear = DropDownList1.SelectedValue.ToString();
        lblFYear.Text = getYear;
        lblSYear.Text = getYear;
        CreateReport(Convert.ToInt32(getYear));
    }
}
